package com.cskaoyan.service.wx.catalog;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.vo.wx.catalog.WxCurrentCatalogVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: erdonc
 * @date: 2022/9/11 15:56
 * TODO:
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    MarketCategoryMapper categoryMapper;


    /**
     * @Author: erdonc
     * @date : 2022/9/11 16:47
     * TODO: 获取商品分类列表和当前分类下的商品信息
     */
    @Override
    public WxCurrentCatalogVo index() {
        // 1. 查询categoryList
        MarketCategoryExample example1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andPidEqualTo(0);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(example1);

        // 2. 查询 currentCategory
        // 从分类列表categoryList中获取第一个(居家)的商品信息
        MarketCategory currentCategory = categoryList.get(0);


        // 3. 查询currentSubCategory
        MarketCategoryExample example2 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andPidEqualTo(currentCategory.getId());
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(example2);

        // 把上面查询到的数据进行封装
        WxCurrentCatalogVo catalogVo = new WxCurrentCatalogVo(currentCategory, categoryList, currentSubCategory);
        return catalogVo;
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/11 16:46
     * TODO: 获取当前分类下的商品信息
     */
    @Override
    public WxCurrentCatalogVo current(Integer id) {
        // 查询 currentCategory
        MarketCategory currentCategory = categoryMapper.selectByPrimaryKey(id);

        // 查询currentSubCategory
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(example);

        // 把上面查询到的数据进行封装
        WxCurrentCatalogVo catalogVo = new WxCurrentCatalogVo(currentCategory, null, currentSubCategory);
        return catalogVo;
    }
}
